package com.admin.scroller.photoorvideo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.admin.scroller.photoorvideo.callback.OnVideoStartAndEndChangedListener;
import com.admin.scroller.photoorvideo.view.VideoProgressView;

/**
 * 视频剪裁页面，待完善
 */
public class VideoCopActivity extends AppCompatActivity implements View.OnClickListener {

    private VideoProgressView progressView;
    private VideoView videoView;
    private TextView tvDuration;
    private TextView tvCropDruation;
    private LinearLayout linearBack;
    private TextView tvVideoCrop;

    private int duration;
    private int orderStart;
    private int orderEnd;

    private String url;

    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0x301){
                long c=videoView.getCurrentPosition();
                progressView.setVideoProgress(c);
                //达到限定结束时间，重新播放
                if(c>=orderEnd){
                    videoView.pause();
                    videoView.seekTo(orderStart);
                    videoView.start();
                }
                handler.sendEmptyMessageDelayed(0X301,200);
            }
            return false;
        }
    });


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cop_video);

        progressView=findViewById(R.id.vpv_video);
        videoView=findViewById(R.id.vv_video);
        tvDuration=findViewById(R.id.tv_video_duration);
        tvCropDruation=findViewById(R.id.tv_crop_duration);
        linearBack=findViewById(R.id.linear_back);
        tvVideoCrop=findViewById(R.id.tv_finish);

        linearBack.setOnClickListener(this);
        tvVideoCrop.setOnClickListener(this);

        url=getIntent().getStringExtra("url");

        progressView.setVideoPath(url);
        progressView.setVideoStartAndEndChangedListener(listener);

        videoView.setVideoPath(url);
        orderStart=0;
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                duration=mp.getDuration();
                if(orderEnd==0){
                    orderEnd=duration;
                }
                progressView.setVideoDuration(duration);
                videoView.start();
                handler.sendEmptyMessage(0x301);
                tvDuration.setText(duration/1000+"秒");
                tvCropDruation.setText(duration/1000+"秒");
            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                videoView.pause();
                videoView.seekTo(orderStart);
                videoView.start();
            }
        });
    }


    private OnVideoStartAndEndChangedListener listener=new OnVideoStartAndEndChangedListener() {
        @Override
        public void onStartChanged() {
            videoView.pause();
        }

        @Override
        public void onNewStartEndbounday(final int start, final int end) {
            orderStart=start;
            orderEnd=end;
            videoView.seekTo(orderStart);
            videoView.start();
            handler.post(new Runnable() {
                @SuppressLint("SetTextI18n")
                @Override
                public void run() {
                    tvCropDruation.setText((end-start)/1000+"秒");
                }
            });
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        if(videoView.isPlaying()){
            videoView.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(videoView!=null&&videoView.isPlaying()){
            videoView.pause();
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.linear_back){
            cropVideo(url);
        }else if(v.getId()==R.id.tv_finish){
//            video.clip(orderStart/1000,(orderEnd-orderStart)/1000);

            final String outPath=Environment.getExternalStorageDirectory().getPath()+"/photovideo/"+System.currentTimeMillis()+".mp4";
//           String cmd="-ss "+start+" -t "+end+" -i "+video+" -vcodec copy -acodec copy "+out;
        }
    }

    private void cropVideo(String path){
        Intent intent=new Intent();
        intent.putExtra("result",path);
        setResult(RESULT_OK,intent);
        finish();
    }
}
